﻿using Language.Resource;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using Swashbuckle.AspNetCore.Annotations;
using System.Globalization;
using VisionCloud.Domain.Filters;

namespace VisionCloud.Api.Controllers.Sys
{
    /// <summary>
    /// 本地化Demo
    /// </summary>
    [SystemModule("本地化")]
    [ApiAuthorize(PolicyEnum.RequireRolePermission)]
    [ApiVersion("1.0")]
    [Route("api/sys/localization")]
    [ApiExplorerSettings(GroupName = "本地化")]
    public class LocalizationController : ControllerBase
    {
        private readonly IStringLocalizer<CommonResource> localizer;

        public LocalizationController(IStringLocalizer<CommonResource> localizer)
        {
            this.localizer = localizer;
        }

        /// <summary>
        /// 中英文切换测试
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet("test"), SwaggerOperation(Tags = new[] { "管理" })]
        public IActionResult Index()
        {
            var cul = CultureInfo.CurrentCulture;
            var str = localizer["LocalizationDemo"].Value;
            return Ok(new { Success = str, Culture = cul,Info = localizer["LocalizationDemo"] });
        }

        ///// <summary>
        ///// 设置系统默认语言
        ///// </summary>
        ///// <param name="culture"></param>
        ///// <remarks>调用此接口后，无需再请求URL中加入参数Culture</remarks>
        ///// <returns></returns>
        //[AllowAnonymous]
        //[HttpGet("setculture"), SwaggerOperation(Tags = new[] { "管理" })]
        //public ApiResult SetCulture(string culture = "en-US")
        //{
        //    SetCookie(culture);
        //    return new ApiResult(Domain.Common.AjaxCode.Success);
        //}

        ///// <summary>
        ///// 设置Cookie
        ///// </summary>
        ///// <param name="culture"></param>
        //private void SetCookie(string culture) => Response.Cookies.Append(
        //       CookieRequestCultureProvider.DefaultCookieName,
        //       CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
        //       new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
        //   );

        ///// <summary>
        ///// 设置系统默认语言并跳转到指定的页面
        ///// </summary>
        ///// <param name="dto"></param>
        ///// <remarks>调用此接口后，无需再请求URL中加入参数Culture</remarks>
        ///// <returns></returns>
        //[AllowAnonymous]
        //[HttpPost("setlanguage"), SwaggerOperation(Tags = new[] { "管理" })]
        //public IActionResult SetLanguage([FromBody] SysLanguageDto dto)
        //{
        //    SetCookie(dto.Culture);
        //    return LocalRedirect(dto.ReturnUrl);
        //}
    }
}
